package com.fanyouzhao.eshop.portal.controller;

import com.fanyouzhao.eshop.core.groups.AddUserGroup;
import com.fanyouzhao.eshop.core.groups.UpdateUserGroup;
import com.fanyouzhao.eshop.core.pojo.User;
import com.fanyouzhao.eshop.core.utils.JsonResult;
import com.fanyouzhao.eshop.portal.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * @author: fanyouzhao
 * @date: 9:49 2021/6/28
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@Validated
public class UserController {

    @Resource
    UserService userService;

    @ApiImplicitParam(name = "id",value = "id",required = true)
    @ApiOperation(value = "userById 通过id获得用户信息")
    @PostMapping("/userById")
    public JsonResult get(@RequestParam("id")Integer id){
        System.out.println("id = " + id);
        User user = userService.selectById(id);
        return new JsonResult("1","sucess",user);
    }

    @ApiOperation(value = "addUser 添加(注册)用户")
    @PostMapping("/addUser")
    public JsonResult add(@Validated(AddUserGroup.class) @RequestBody User user){
        System.out.println("user = " + user);
        userService.add(user);
        User userBack = userService.selectById(user.getId());
        return new JsonResult("1","sucess",userBack);
    }

    @ApiOperation(value = "updateUser 修改用户信息")
    @PostMapping("/updateUser")
    public JsonResult updateUser(@Validated(UpdateUserGroup.class) @RequestBody User user){
        System.out.println("user = " + user);
        userService.updateById(user);
        return new JsonResult("1","sucess",user);
    }

    @PostMapping("/userByPage")
    public JsonResult selectByPage(
            @Min(value = 1,message = "页码最小不能小于1") @RequestParam("page") Integer page,
            @Min(value = 1,message = "每页大小最小不能小于1") @RequestParam("size") Integer size){
        PageInfo<User> userPageInfo = userService.selectByPage(page, size);
        return new JsonResult("1","查询成功",userPageInfo);
    }
}
